package com.wangsir.servlet;

import com.wangsir.entity.Clazz;
import com.wangsir.service.AdminService;
import com.wangsir.service.ClazzService;
import com.wangsir.service.StudentService;
import com.wangsir.util.ApiResult;
import com.wangsir.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    StudentService studentService = new StudentService();
    ClazzService clazzService = new ClazzService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收请求的编码格式
        req.setCharacterEncoding("utf-8");
        //返回数据的编码格式
        resp.setContentType("application/json;charset=utf-8");
        int clazzC = clazzService.count();
        int stuC = studentService.count();
        List<Clazz> statistics = clazzService.statistics();
        //将上面三个数据返回给前端，但因为没有对应的实体类，所以将其封装在map集合中，进行返回给前端
        Map<String,Object> map = new HashMap<>();
        map.put("clazzCount",clazzC);
        map.put("studentCount",stuC);
        map.put("statistics",statistics);
        resp.getWriter().write(ApiResult.json(true,"成功",map));
    }
}
